---计划的执行者，负责定制并执行计划

---@class Performer
---@field planHandler PlanHandler
---@field planner Planner
---@field goalManager GoalManagerBase
---@field actionManager ActionManagerBase
Performer = class()

---@param agent AgentBase
function Performer:ctor(agent)
    self.planHandler = PlanHandler.new();
    self.planHandler:SetCompleteCallBack(function ()
        self:PlanCmplete();
    end)
    self.planner = Planner.new(agent);
    self.goalManager = agent.goalManager;
    self.actionManager = agent.actionManager;
    self.actionManager:SetActionComplenteCallFunc(function (label)
        self:PlanActionComplete(label);
    end)
end

---打断计划
function Performer:Interruptible()
    Debug.Log("打断计划");
    self.planHandler:Interruptible();
    self:TryInterruptAction();
end

---尝试中断当前计划执行的动作
function Performer:TryInterruptAction()
    self.planHandler:GetCurrentHandler():TryInterruptAction();
end

---数据更新执行
function Performer:UpdateData()
    if self:WhetherToReplan() then
        Debug.Log("制定新计划");
        self:BuildPlanAndStart();
    end
end

---制定并开始计划
function Performer:BuildPlanAndStart()
    if self.goalManager.currentGoal ~= nil then
        Debug.Log("-----------新的目标："..self.goalManager.currentGoal.label);
    end
    local plan = self.planner:BuildPlan(self.goalManager.currentGoal);
    if plan ~= nil and plan:Size() > 0 then
        self.planHandler:Init(self.actionManager,plan);
        self.planHandler:StartPlan();
        self.actionManager.isPerformAction = true;
    end
end

---动作执行完成回调
function Performer:PlanActionComplete(actionLaber)
    if self.planHandler:GetCurrentHandler().label == actionLaber then
        Debug.Log("计划进入下一步动作");
        self.planHandler:NextAction();
    end
end

---完成回调
function Performer:PlanCmplete()
    Debug.Log("计划完成！");
    self.actionManager.isPerformAction = false;
end

---检测是否需要重新制定计划
function Performer:WhetherToReplan()
    ---当前计划是否完成
    return self.planHandler:IsComplete();
end